﻿$(document).ready(function() {
	var navul = document.getElementById("navul");
	var navlis = navul.getElementsByTagName('li');
	navul.onclick = function(e) {
		if (navlis[0].className != 'liShow') {
			for(var i = 0, n = navlis.length; i < n; i++) {
				navlis[i].className = 'liShow';
			}
		} else {
			for(var i = 0, n = navlis.length; i < n; i++) {
				navlis[i].className = '';
			}
		}
		e.stopPropagation();
	}
	document.body.onclick = function() {
		for(var i = 0, n = navlis.length; i < n; i++) {
			navlis[i].className = '';
		}
	}

	/* 首次打开加载home */
	var $content = $('#content');
	$content.load('home.xml');
	/* ajax页面局部刷新 */
	$('nav li:nth-of-type(1) a').click(function() {
		$content.load('home.xml');
		if($content.hasClass('rlgbackground')) {
			$content.removeClass('rlgbackground');
		}
	});
	$('nav li:nth-of-type(2) a').click(function() {
		$content.load('index2.xml');
		if($content.hasClass('rlgbackground')) {
			$content.removeClass('rlgbackground');
		}
	});
	$('nav li:nth-of-type(3) a').click(function() {
		$content.load('index3.xml').addClass('rlgbackground');
		/* 图片宽度 */
		var imgH;
		var beforeH = 0;
		var t = setInterval(function () {
			imgH = $('.film .face img').height();
			if(imgH > beforeH) {
				beforeH = imgH;
			} else {
				clearInterval(t);
				$('.container').height(imgH);
				$('.back').height(imgH).width($('.film .face img').width());
			}
		}, 500);
		
	});


	/* 菜单滚动到下面后固定菜单 */
	var targetH = document.getElementsByTagName('header')[0].offsetHeight - 20;
	var curH;
	var timer;
	if (window.innerWidth < 768) {
		timer = setInterval(function () {
			curH = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;
			if(curH > targetH) {
				navul.style.position = "absolute";
				navul.style.top = curH - targetH + 70 + "px";
			} else {
				navul.style.position = "absolute";
				navul.style.top = 3 + 'px';
			}
		}, 500);
	}

	window.onresize = function(){
		// timer不清除，被替代的的定时器也会一直存在，即使timer被重新定义
		clearInterval(timer);
		targetH = document.getElementsByTagName('header')[0].offsetHeight - 20;
		if (window.innerWidth < 768) {
			timer = setInterval(function () {
				curH = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;
				if(curH > targetH) {
					navul.style.position = "absolute";
					navul.style.top = curH - targetH + 70 + "px";
				} else {
					navul.style.position = "absolute";
					navul.style.top = 3 + 'px';
				}
			}, 500);
		} else {
			clearInterval(timer);
			navul.style.position = "inherit";
			// navul.style.top = 0;
		}

		// 图片
		$('.container').height($('.film .face img').height());
		$('.back').height($('.film .face img').height()).width($('.film .face img').width());
	}
});